#!/usr/bin/perl
# Version 0.2.1

$testcase = "addr";

# 1 = PASS
# 2 = FAIL
# 3 = INCONCLUSIVE
$verdict = 3;

$dumpfile = "$testcase.dump";
$logfile = "$testcase.log";

if ($#ARGV ne 1) {
        print("Usage: $testcase.pl dump_file log_file");
        exit 1;
}

$dumpfile = $ARGV[0];
$logfile = $ARGV[1];

#------------------------------------------------------------------------------
# Only look at the packets sent from the RUT (0.0.0.1)

# Open the dumpfile
$success = 1;
open(DUMPFILE, $dumpfile) or $success = 0;

$verdict_1 = 3; # the verdict for the timing part
$verdict_2 = 3; # the verdict for the addressing part

$lts = -1;

if ($success eq 1) {
	while(<DUMPFILE>) {
		# only look at hello packets, originated by router 0.0.0.1
		if(/(.*) 0\.0\.0\.1 > (.*): OSPFv2-hello (.*)/) {
			@al = split(/ /, $_);
			
			# Look at destination address
			if($al[3] eq "224.0.0.5:") {
				if($verdict_2 ne 2) {
					$verdict_2 = 1;
				}	
			} else {
				$verdict_2 = 2;
			}
			
			
			# now look at the time
			@ti = split(/\./, $al[0]);
			@ts = split(/:/, $ti[0]);
			
			$secs = $ts[2] + ($ts[1] + $ts[0] * 60) * 60;
			if ($lts ne -1) {
				if( $_ =~ /int (\d*)/ ) {
					if($secs eq ($1 + $lts)) {
						if( $verdict_1 ne 2 ) {
							$verdict_1 = 1;
						}
					} else {
						$verdict_1 = 2;
					}
				}
			}
			
			$lts = $secs;
		}	
	}
	close(DUMPFILE);
}



# finally evaluate results and generate report
if ($verdict_1 eq 1) {
	print("Hello packets are sent every HelloInterval seconds. Test PASSED.\n");
} elsif ($verdict_1 eq 2) {
	print("Hello packets are sent in intervals different from HelloInterval. Test FAILED.\n");
} else {
	print("Cannot determine the intervals at which Hello packets are sent. Test INCONCLUSIVE.\n");
}

if ($verdict_2 eq 1) {
	print("Hello packets are sent to AllSPFRouters (224.0.0.5). Test PASSED.\n");
} elsif ($verdict_2 eq 2) {
	print("Hello packets are not sent to AllSPFRouters (224.0.0.5). Test FAILED.\n");
} else {
	print("Cannot determine the destination address for Hello packets. Test INCONCLUSIVE.\n");
}


$verdict = $verdict_1;
if ($verdict_2 ne $verdict) {
	if($verdict_2 eq 2) {
		$verdict = 2;
	} elsif ($verdict_2 eq 3) {
		if($verdict ne 2) {
			$verdict = 3;
		}
	}	
}

#------------------------------------------------------------------------------
print("### VERDICT for $testcase: ");

if ($verdict eq 1) { 
        print("PASS"); 
} elsif ($verdict eq 2) {
        print("FAIL");
} else {
        print("INCONCLUSIVE");
}

print(" ###\n");